package model.graphic.objects;

import controller.Constants;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import model.graphic.GraphicModel;
import model.graphic.locations.Location;

/**
 *
 * @author Ben
 */
public class GStoreNode extends GNode{
    private Graphical outport = null;
    private GraphicModel gmodel;
    private boolean mustResize = false;

    public GStoreNode(String value, GraphicModel gmodel) {
        super(value);
        this.gmodel = gmodel;
        this.setHeight(GOutput.ARROW_DELTA * 2);
        this.setWidth(40);
    }

    @Override
    public int getClickLocation(int y) {
        return 2;//pretend we clicked the value field of the GNode
    }

    @Override
    public void setValue(String value) {
        if(outport == null){
            outport = (Graphical)gmodel.getAttachedObjects(this, Constants.getOutputportString()).toArray()[0];
        }
        ((GOutputPort)outport).setName(value);
        mustResize = true;
        super.setValue(value);
    }

    @Override
    public Location getLowerRight() {
        return lowerLeft;
    }

    @Override
    public void paint(Graphics2D g) {
        //change the node width if the port is done with adapting to the new name
        if(mustResize && outport != null && !((AbstractOutPort)outport).isMustResize()){
              this.setWidth(outport.getWidth() + GOutput.ARROW_DELTA);
              mustResize = false;
        }
    }
}
